package com.zkingsoft.plugin.util.wx;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;

public class JsapiTicket
{
  private static JsapiTicket jsapiTicket = null;
  private String jsapi_Ticket;
  private static long endTime;

  public static String getJsapiTicket()
  {
    if (jsapiTicket == null) {
      jsapiTicket = new JsapiTicket();
      jsapiTicket.makeJsapiTicket();
    }
    long nowTime = System.currentTimeMillis();
    if (nowTime > endTime) {
      jsapiTicket.makeJsapiTicket();
    }
    return jsapiTicket.jsapi_Ticket;
  }

  public void makeJsapiTicket() {
    HttpRequest request = new HttpRequest();
    Map<String,String> params = new HashMap<String,String>();
    params.put("access_token", AccessToken.getAccessToken());
    params.put("type", "jsapi");
    HttpResponse result = null;
    try {
      result = request.sendHttpsGet(Constants.GETTICKET_URL, params);
    } catch (IOException localIOException) {
    	
    }
    JSONObject json = JSONObject.fromObject(result.getDataString());
    endTime = System.currentTimeMillis() + 7100000L;
    this.jsapi_Ticket = json.getString("ticket");
  }
}
